package com.ThreadStudy.StudyDemo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/*
*
* 练习Thread,实现多线程同步下载
*
* */
public class TestThreadDown extends Thread{

    private String url; // 网络图片的地址
    private String name; // 保存的文件名

    public  TestThreadDown(String url,String name){
        this.url=url;
        this.name=name;
    }

    // 下载图片线程的执行体
    @Override
    public void run() {
      WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
    }


    public static void main(String[] args) {
        TestThreadDown t = new TestThreadDown("https://img2020.cnblogs.com/blog/2508133/202109/2508133-20210920182859176-1378059383.png","1.png");
        TestThreadDown t1 = new TestThreadDown("https://img2020.cnblogs.com/blog/2508133/202109/2508133-20210920182847694-1327752584.png","2.png");
        TestThreadDown t2 = new TestThreadDown("https://img2020.cnblogs.com/blog/2508133/202109/2508133-20210920183237097-1882601877.png","3.png");

        /*
         * 下载了文件名为:3.png
         * 下载了文件名为:2.png
         * 下载了文件名为:1.png
         * */

        t.start();
        t1.start();
        t2.start();

    }
}


// 下载器
class WebDownloader{
    // 下载方法
    public void downloader(String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现的问题");
        }


    }
}

